﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EmpWorkEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id) && id != "undefined")
                {
                    var v = ctx.EmpWorks.Single(r => r.Id == int.Parse(id));
                    StartDate.Value = v.StartDate.ToString("yyyy-MM-dd");
                    if (v.EndDate != null)
                    {
                        EndDate.Value = ((DateTime)v.EndDate).ToString("yyyy-MM-dd");
                    }
                    Corporation.Value = v.Corporation;
                    Address.Value = v.Address;
                    Department.Value = v.Department;
                    txtPosition.Value = v.Position;
                    Responsibility.Value = v.Responsibility;
                    Achievement.Value = v.Achievement;
                    Salary.Value = v.Salary;
                    LeaveCause.Value = v.LeaveCause;
                    Certifier.Value = v.Certifier;
                    CertifierPhone.Value = v.CertifierPhone;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string id = Request.QueryString["id"];
            EmpWork empWork;
            if (!string.IsNullOrEmpty(id) && id != "undefined")
            {
                empWork = ctx.EmpWorks.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                empWork = new EmpWork();
                empWork.EmpId = int.Parse(Request.QueryString["empId"]);
                ctx.EmpWorks.InsertOnSubmit(empWork);
            }
            empWork.StartDate = DateTime.Parse(StartDate.Value);
            if(!string.IsNullOrWhiteSpace(EndDate.Value))
            {
                empWork.EndDate = DateTime.Parse(EndDate.Value);
            }
            empWork.Corporation = Corporation.Value;
            empWork.Address = Address.Value;
            empWork.Department = Department.Value;
            empWork.Position = txtPosition.Value;
            empWork.Responsibility = Responsibility.Value;
            empWork.Achievement = Achievement.Value;
            empWork.Salary = Salary.Value;
            empWork.LeaveCause = LeaveCause.Value;
            empWork.Certifier = Certifier.Value;
            empWork.CertifierPhone = CertifierPhone.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + empWork.Id + "';window.close();</script>");
        }
    }
}